package com.xunwulian.web.controller.buss;


import com.xunwulian.base.dao.CardInfoDao;
import com.xunwulian.base.entity.MonthlyOpenCardEntity;
import com.xunwulian.base.entity.MonthlySmsEntity;
import com.xunwulian.base.util.DateStyle;
import com.xunwulian.base.util.DateUtil;
import com.xunwulian.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping(value = "/echart")
public class EchartController extends BaseController {

    @Autowired
    private CardInfoDao cardInfoDao;


    /**
     * 每月开发激活情况
     *
     * @return
     */
    @RequestMapping(value = "/monthlyOpenCardInfo")
    @ResponseBody
    public Object monthlyOpenCardInfo() {

        // 数据分组
        List<String> legend = new ArrayList(Arrays.asList(new String[]{"开卡数"}));
        // 横坐标
        List<String> category = new ArrayList();
        // 纵坐标
        List<Series> series = new ArrayList();

        try {
            List<MonthlyOpenCardEntity> openCardEntityList = cardInfoDao.monthlyOpenCardInfo();

            Date date = DateUtil.addMonth(new Date(), -11);
            ArrayList<Long> openCardData = new ArrayList();
            for (int i = 0; i < 12; i++) {
                // 横坐标
                String month = DateUtil.DateToString(date, DateStyle.YYYY_MM);
                category.add(month);

                Optional<MonthlyOpenCardEntity> r = openCardEntityList.stream().filter(p -> month.equals(p.getMonth()
                )).findFirst();
                openCardData.add(r.isPresent() ? r.get().getCount() : 0L);

                date = DateUtil.addMonth(date, 1);
            }

            series.add(new Series("开卡数", "line", openCardData));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new EchartData(legend, category, series);
    }

    /**
     * 每月收发短信统计
     *
     */
    @RequestMapping(value = "/monthlySmsInfo")
    @ResponseBody
    public Object monthlySmsInfo() {

        // 数据分组
        List<String> legend = new ArrayList<>(Arrays.asList(new String[]{"发送", "接收"}));
        // 横坐标
        List<String> category = new ArrayList();

        // 纵坐标
        List<Series> series = new ArrayList();
        ArrayList<Long> sendData = new ArrayList();
        ArrayList<Long> recvData = new ArrayList();

        try {
            List<MonthlySmsEntity> smsEntityList = cardInfoDao.monthlySmsInfo();

            Date date = DateUtil.addMonth(new Date(), -11);
            for (int i = 0; i < 12; i++) {
                // 横坐标
                String month = DateUtil.DateToString(date, DateStyle.YYYY_MM);
                category.add(month);

                // 发送数据
                Optional<MonthlySmsEntity> s = smsEntityList.stream().filter(p -> p.getType() == 1).filter(p -> p
                        .getMonth().equals(month)).findFirst();
                sendData.add(s.isPresent() ? s.get().getCount() : 0L);

                // 接收数据
                Optional<MonthlySmsEntity> r = smsEntityList.stream().filter(p -> p.getType() == 2).filter(p -> p
                        .getMonth().equals(month)).findFirst();
                recvData.add(r.isPresent() ? r.get().getCount() : 0L);

                date = DateUtil.addMonth(date, 1);
            }
            series.add(new Series("发送", "line", sendData));
            series.add(new Series("接收", "line", recvData));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return new EchartData(legend, category, series);
    }

}
